package com.wwj.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wwj.gulimall.product.entity.SpuInfoEntity;
import com.wwj.gulimall.product.vo.SpuSaveVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.common.utils.PageUtils;
import com.wwj.common.utils.Query;

import com.wwj.gulimall.product.dao.SkuInfoDao;
import com.wwj.gulimall.product.entity.SkuInfoEntity;
import com.wwj.gulimall.product.service.SkuInfoService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {

        LambdaQueryWrapper<SkuInfoEntity> wrapper = new LambdaQueryWrapper<>();

        // 检索条件加入关键字
        String key = (String) params.get("key");
        if (StringUtils.isNotBlank(key)){
            wrapper.and((item) ->{
                item.eq(SkuInfoEntity::getSkuId, key).or()
                        .like(SkuInfoEntity::getSkuName, key);
            });
        }

        // 检索条件加入分类id
        String catelogId = (String) params.get("catelogId");
        if (StringUtils.isNotBlank(catelogId)){
            int catID = Integer.parseInt(catelogId);
            if (catID > 0){
                wrapper.eq(SkuInfoEntity::getCatalogId, catID);
            }
        }

        // 检索条件加入品牌id
        String brandId = (String) params.get("brandId");
        if (StringUtils.isNotBlank(brandId)){
            int braId = Integer.parseInt(brandId);
            if (braId > 0){
                wrapper.eq(SkuInfoEntity::getBrandId, braId);
            }
        }

        // 检索条件加入价格区间
        String min = (String) params.get("min");
        String max = (String) params.get("max");
        if (StringUtils.isNotBlank(min) && StringUtils.isNotBlank(max)){
            BigDecimal Imin = new BigDecimal(min);
            BigDecimal Imax = new BigDecimal(max);
            if (Imax.compareTo(new BigDecimal(0)) > 0 && Imax.compareTo(Imin) > 0){
                wrapper.between(SkuInfoEntity::getPrice, Imin, Imax);
            }else {
                wrapper.ge(SkuInfoEntity::getPrice, Imin);
            }
        }

        IPage<SkuInfoEntity> page = this.page(new Query<SkuInfoEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

}